package com.dingding.gmall.order.config;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * @author liugongding
 * @description:
 * @date 2022/3/14 9:12 PM
 */

@Configuration
public class SeataConfig  {

    @Autowired
    DataSourceProperties dataSourceProperties;


    //@Bean
    //public DataSource dataSource(DataSourceProperties dataSourceProperties) {
    //    HikariDataSource dataSource = dataSourceProperties
    //            .initializeDataSourceBuilder()
    //            .type(HikariDataSource.class)
    //            .build();
    //    if (StringUtils.hasText(dataSourceProperties.getName())) {
    //        dataSource.setPoolName(dataSourceProperties.getName());
    //    }
    //    return new DataSourceProxy(dataSource);
    //}

    @Bean
    public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws IOException {
        // 这里用 MybatisSqlSessionFactoryBean 代替了 SqlSessionFactoryBean，否则 MyBatisPlus 不会生效
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dataSource);
        mybatisSqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:/mapper/**/*.xml"));
        return mybatisSqlSessionFactoryBean;
    }
}
